package com.bocim.course.controller;

import com.bocim.course.meta.Product;
import com.bocim.course.meta.User;
import com.bocim.course.service.ProductService;
import com.bocim.course.service.UserService;
import com.bocim.course.serviceImp.IProductService;
import com.bocim.course.serviceImp.IUserService;
import com.bocim.course.utility.EncodingTool;
import com.sun.javafx.css.converters.EnumConverter;
import com.sun.javafx.sg.prism.NGShape;
import com.sun.media.sound.ModelPatch;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by liujunchen on 2016/11/6.
 */
@Controller
@RequestMapping("/")
public class LogonFormController {
    @Autowired
    private  IProductService  productService ;
    @Autowired
    private  IUserService  userService   ;
    //主页
    @RequestMapping(value = "/")
    public ModelAndView   indexpage(ModelAndView  index    , HttpServletRequest request
    ,HttpServletResponse response
    ){
        if (request.getParameter("type")  ==null ){
            request.setAttribute("type" , 0 );
        }
        else
        {
            int type = Integer.parseInt(request.getParameter("type"));
            request.setAttribute("type",type);
        }

        //获得对应的session user的信息
        User user  = (User) request.getSession().getAttribute("user") ;
//        System.out.println(request.getParameter("type"));
        //得到全部的商品的信息
//        IProductService productService  =  new ProductService()  ;
        List<Product> productList = productService.selectAllProduct();
        //把信息传递到对应的model中
        index.setViewName("index");
        index.addObject("productList",productList);
        index.addObject("user",user)  ;
        return index ;
    }
    @RequestMapping("/login")
    public ModelMap    handle(HttpSession session ,ModelMap modelMap){
        session.getAttribute("user") ;
        modelMap.addAttribute("user");
        return modelMap ;
    }
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public   String skiptologin(HttpSession session ){
        session.invalidate();
        return "login";
    }
    //商品展示页面controller
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public  ModelMap  showproduct(@RequestParam String id , ModelMap modelMap ,HttpSession session){
        int productId  = Integer.parseInt(id);
        //得到对应的用户
        User user = (User) session.getAttribute("user");

//        IProductService productService = new ProductService();
        Product product = productService.selectById(productId, 0);
        modelMap.addAttribute("product" ,product)  ;
        modelMap.addAttribute("user",user);
        return    modelMap;
    }

    @RequestMapping(value = "/account",method = RequestMethod.GET)
    public ModelMap showaccount(ModelMap modelMap , HttpSession session ){
        //从session中得到对应的session得到对应的用户
        User user = (User) session.getAttribute("user");
        int userId  =  user.getUserId();
        if(user!=null)
        {
//            IProductService productService = new ProductService();
            //得到对应的用户购物清单
            List<Product> buyList = productService.selelctProductListByuserId(userId)  ;
            modelMap.addAttribute("user",user)  ;
            modelMap.addAttribute("buyList",buyList)  ;
        }
        return modelMap ;
    }
    @RequestMapping("/public")
    public  ModelMap showpublish(HttpSession session , ModelMap modelMap ){
        User user = (User) session.getAttribute("user") ;
        modelMap.addAttribute("user")  ;
        return modelMap ;
    }

    @RequestMapping(value = "/publicSubmit" ,method = RequestMethod.POST)
    public  ModelMap showPublicSubmit(HttpSession session , ModelMap modelMap,
                                      @RequestParam String title , @RequestParam String summary,
                                      @RequestParam String detail,@RequestParam String image,@RequestParam double price){

        User user = (User) session.getAttribute("user") ;
        Product product = new Product();
        product.setTitle(EncodingTool.encodeStr(title));
        product.setSummary(EncodingTool.encodeStr(summary));
        product.setPrice(price);
        product.setDetail(EncodingTool.encodeStr(detail));
        product.setImage(EncodingTool.encodeStr(image));
        product.setPublishUserId(user.getUserId());
//        IProductService productService = new ProductService();
        modelMap.addAttribute("user", user);
        if (productService.insertProduct(product)) {
            modelMap.addAttribute("product", product);
        }
        return modelMap ;
    }
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public  ModelMap showEditPage(HttpSession session  , ModelMap modelMap , @RequestParam("id") int productId ){
        User user = (User) session.getAttribute("user") ;
//        IProductService    productService = new ProductService()  ;
        Product product =  productService.selectById(productId,0)  ;
        modelMap.addAttribute("product",product);
        modelMap.addAttribute("user",user) ;
        return  modelMap ;
    }

    @RequestMapping(value = "/editSubmit" ,method = RequestMethod.POST)
    public  ModelMap showEditSubmit(HttpSession session , ModelMap modelMap ,
                                     @RequestParam("id") int  productId   ,
                                      @RequestParam String title , @RequestParam String summary,
                                      @RequestParam String detail,@RequestParam String image,@RequestParam double price) {

        User user = (User) session.getAttribute("user");

        Product product = new Product();
        product.setId(productId);
        product.setTitle(EncodingTool.encodeStr(title));
        product.setSummary(EncodingTool.encodeStr(summary));
        product.setPrice(price);
        product.setDetail(EncodingTool.encodeStr(detail));
        product.setImage(EncodingTool.encodeStr(image));
//        IProductService productService = new ProductService() ;
        if (productService.updateProduct(product))
            modelMap.addAttribute("product" , product)  ;
        modelMap.addAttribute("user", user)  ;
        modelMap.addAttribute("productId" , productId )  ;
        return     modelMap ;
    }
}
